﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model;
using Planer.Logic;
using Planer.UI.Forms;

namespace Planer.UI
{
    public partial class LoginForm : Form
    {
        private user currentUser = null;

        public user CurrentUser
        {
            get { return currentUser; }
        }

        public LoginForm()
        {
            InitializeComponent();
            CustomInitializeComponent();
        }

        private void CustomInitializeComponent()
        {
            UserModel model = UserModel.GetInstance();
            List<user> users = model.GetUserList();
            if (users.Count == 0)
            {
                panelLogin.Enabled = false;
                labelNoUser.Visible = true;
                return;
            }

            panelLogin.Enabled = true;
            labelNoUser.Visible = false;

            comboBoxUsers.Items.Clear();
            comboBoxUsers.Items.AddRange(users.ToArray());
            comboBoxUsers.SelectedIndex = 0;
            textBoxPass.Focus();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            Authorization();
        }

        private void Authorization()
        {
            string pass = textBoxPass.Text;
            if (pass == "")
            {
                MessageBoxUtils.ShowWarningBox("Введите пароль");
                return;
            }
            user _user = (user)comboBoxUsers.SelectedItem;
            if (pass != _user.password_hash)
            {
                textBoxPass.Text = "";
                MessageBoxUtils.ShowErrorBox("Неверный пароль!");
                return;
            }

            currentUser = _user;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void textBoxPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Authorization();
                e.Handled = true;
            }
        }

        private void linkLabelRestorePass_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (comboBoxUsers.SelectedIndex < 0)
            {
                return;
            }

            user _user = (user)comboBoxUsers.SelectedItem;
            try
            {
                MailSender.SendMail("Ваш пароль: " + _user.password_hash, "BudgetPlaner - Восстановление пароля", _user.email, _user.name);
                MessageBoxUtils.ShowWarningBox(_user.name + ", пароль отправлен Вам на email");
            }
            catch (Exception ex)
            {
                MessageBoxUtils.ShowErrorBox(ex.Message);
            }
        }

        private void linkLabelRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RegisterUserForm frm = new RegisterUserForm();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                CustomInitializeComponent();
            }
        }

    }
}
